package com.kehongborui.fliggy.service.impl;

import com.kehongborui.fliggy.entity.User;
import com.kehongborui.fliggy.exception.FliggyException;
import com.kehongborui.fliggy.mapper.UserMapper;
import com.kehongborui.fliggy.service.UserService;
import com.kehongborui.fliggy.util.CommonUtils;
import com.kehongborui.fliggy.util.JedisUtil;
import com.kehongborui.fliggy.util.MailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import java.security.GeneralSecurityException;

import static com.kehongborui.fliggy.enums.ExceptionMessage.EMAIL_IS_EXIST;
import static com.kehongborui.fliggy.enums.ExceptionMessage.TEL_IS_EXIST;
import static com.kehongborui.fliggy.enums.SysConfig.EMAIL_AUTH_EXP_TIME;
import static com.kehongborui.fliggy.enums.SysConfig.EMAIL_INCR_KEY;

/**
 * @author zhang peng yang ming
 * @CopyRight (C) http://www.csdn.com
 * @brief
 * @email 122162005@qq.com
 * @date 2018-09-05
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private JedisUtil jedisUtil;

    @Override
    public User findByTel(String tel) {
        return userMapper.selectByTelOrUsernameOrEmail(tel, null, null);
    }

    @Override
    public User findByEmail(String email) {
        return userMapper.selectByTelOrUsernameOrEmail(null, email, null);
    }

    @Override
    public boolean create(User user) {
        User retUser = userMapper.selectByTelOrUsernameOrEmail(null, user.getEmail(), null);
        // 判断email 对应的用户是否存在
        if (retUser != null) {
            throw new FliggyException(EMAIL_IS_EXIST.getDesc());
        }

        retUser = userMapper.selectByTelOrUsernameOrEmail(user.getTel(), null, null);
        // 判断tel 对应的用户是否存在
        if (retUser != null) {
            throw new FliggyException(TEL_IS_EXIST.getDesc());
        }

        user.setUid(CommonUtils.token());
        int i = userMapper.insertSelective(user);
        jedisUtil.setObject(user.getUid(), user);

        // 用户存在，则返回对应的电话号码，不存在则返回空字符串
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    MailUtil.sendEmail(user.getEmail(), "88c996624f33422aae8e5c962c355a5b");
//                } catch (MessagingException e) {
//                    e.printStackTrace();
//                } catch (GeneralSecurityException e) {
//                    e.printStackTrace();
//                }
//            }
//        });

        // lambda表达式只是一种规则，这种方法相当于上面的方式
        new Thread(()->{
            try {

                long incr = jedisUtil.setIncr(EMAIL_INCR_KEY, 1);
                String token = CommonUtils.token() + incr;
                jedisUtil.set(token, user.getEmail(), EMAIL_AUTH_EXP_TIME);
                // 发送邮件
                MailUtil.sendEmail(user.getEmail(), token);
            } catch (MessagingException e) {
                e.printStackTrace();
            } catch (GeneralSecurityException e) {
                e.printStackTrace();
            }
        }).start();

        return i > 0;
    }

    @Override
    public boolean enabledAccount(String email) {
        int ret = userMapper.enabledByEmail(email);
        // 更新缓存中已存在的数据
        User user = userMapper.selectByTelOrUsernameOrEmail(null, email, null);
        jedisUtil.setObject(user.getUid(), user);
        return ret > 0;
    }

    @Override
    public User findById(String uid) {
        User user = userMapper.selectByPrimaryKey(uid);
        return user;
    }

    @Override
    public void resetAttempts() {
        userMapper.resetAttempts();
    }
}
